﻿using Engine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Game;
using System.Reflection.Emit;
using System.Security.Cryptography;
using System.Collections;

namespace IDManager
{
    public static class BlockIDManager
    {
        public static List<List<IDInfo>> ids = new List<List<IDInfo>>();
        public static BevelledButtonWidget bt;
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialize()
        {
            List<IDInfo> gameID = GetBlockIndices(Assembly.GetAssembly(typeof(AirBlock)));
            ids.Add(gameID);
            foreach (KeyValuePair<string, Assembly> dll in ModsManager.Dlls)
            {
                List<IDInfo> id = GetBlockIndices(dll.Value);
                ids.Add(id);
            }
            ScreensManager.AddScreen("BlockID", new BlockIDScreen());
            AddButton();
            Log.Information("[BlockIDManager] 方块ID获取完成");
        }

        public static void AddButton()
        {
            //按钮
            Screen settingsScreen = ScreensManager.FindScreen<Screen>("Settings");
            bt = new NewButton();
            settingsScreen.Children.Add(bt);
            bt.ParentWidget = settingsScreen.Children.Find<BevelledButtonWidget>("Performance");
            bt.Size = new Vector2(310, 60);
            bt.Margin = new Vector2(310 / 2 + 4, -70);
            bt.Text = "方块ID查重";
            bt.Name = "IDMgr";
            bt.BevelColor = Color.SkyBlue;
            bt.CenterColor = Color.SkyBlue;

            //文字
            Screen mainMenu = ScreensManager.FindScreen<Screen>("MainMenu");
            LabelWidget labelWidget = new LabelWidget();
            mainMenu.Children.Add(labelWidget);
            labelWidget.Text = "Tip: 设置->方块ID查重 可查看方块ID占用情况";
            labelWidget.FontScale = 0.65f;
            labelWidget.ParentWidget = mainMenu.Children.Find<ButtonWidget>("Settings");
            labelWidget.Margin = new Vector2(10, 60);
        }
        public static void Update()
        {

        }


        public static List<IDInfo> GetBlockIndices(Assembly assembly)
        {
            Type blockType = typeof(Block);
            IEnumerable<Type> blockSubtypes = assembly.GetTypes().Where(t => t.IsSubclassOf(blockType) && !t.IsAbstract);
            List<IDInfo> outID = new List<IDInfo>();

            foreach (Type subtype in blockSubtypes)
            {
                FieldInfo indexField = subtype.GetFields(BindingFlags.Public | BindingFlags.Static).FirstOrDefault(f => f.Name == "Index" && f.FieldType == typeof(int) && f.IsLiteral);

                if (indexField != null)
                {
                    outID.Add(new IDInfo((int)indexField.GetValue(null), indexField.ReflectedType.Name, assembly.FullName, ""));
                }
            }

            return outID;
        }
        public static List<IDInfo> GetBlockIndices(string dllFile)
        {
            List<int> indices = new List<int>();
            Assembly assembly = Assembly.LoadFile(dllFile);
            return GetBlockIndices(assembly);
        }
        public static List<IDInfo> FindUseNames(int id)
        {
            List<IDInfo> result = new List<IDInfo>();

            foreach (var list in ids)
            {
                foreach (var idInfo in list)
                {
                    if (idInfo.ID == id)
                    {
                        result.Add(idInfo);
                    }
                }
            }

            return result;
        }
    }
}
